// The NSpring Framework for .NET
// Copyright (c) 2003, Jeffrey Varszegi
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
// 
// - Redistributions of source code must retain the above copyright notice, this 
// list of conditions and the following disclaimer.
// 
// - Redistributions in binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation and/or other 
// materials provided with the distribution.
// 
// - Neither the name of the NSpring project nor the names of its contributors may 
// be used to endorse or promote products derived from this software without 
// specific prior written permission from the copyright owner.
// 
// - No product derived from this software may be called "NSpring", nor may 
// "NSpring" appear in the name of such a product, without specific prior written 
// permission from the copyright owner.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE. 

namespace NSpring.Logging.Filters {

using System;
using System.Collections;

/// <summary>
/// <para>
/// Provides a type of Filter that is composed of other filters.  An Event matches this filter 
/// under the following circumstances:
/// </para>
/// <para>
/// if this filter is inclusive, and the Event matches all children;
/// </para>
/// <para>
/// if this filter is exclusive, and the Event does not match any children.
/// </para>
/// <para>
/// Note that if the filter is inclusive and empty (has no children), all events succeed;
/// if it is exclusive and empty, all events fail.
/// </para>
/// </summary>
public sealed class CompositeFilter : Filter {
    
    private ArrayList filterList;
    private Filter[] filterArray;
    
    private readonly object monitor                     = new object();
    
    /// <summary>
    /// Constructs a new instance
    /// </summary>
	public CompositeFilter() {
		filterList = new ArrayList(20);
		filterArray = new Filter[0];
		isInclusive = true;
	}
	
    /// <summary>
    /// A convenience constructor to allow single-statement
    /// population without the need to pass an array
    /// </summary>
    /// <param name="filter1">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1
        ) : this(new Filter[] {
        filter1
    }) {}
	
  	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2
    ) : this(new Filter[] {
        filter1,
        filter2
    }) {}
	
/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    /// <param name="filter5">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4,
        Filter filter5
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4,
        filter5
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    /// <param name="filter5">A filter to add to the composite filter</param>
    /// <param name="filter6">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4,
        Filter filter5,
        Filter filter6
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4,
        filter5,
        filter6
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    /// <param name="filter5">A filter to add to the composite filter</param>
    /// <param name="filter6">A filter to add to the composite filter</param>
    /// <param name="filter7">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4,
        Filter filter5,
        Filter filter6,
        Filter filter7
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4,
        filter5,
        filter6,
        filter7
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    /// <param name="filter5">A filter to add to the composite filter</param>
    /// <param name="filter6">A filter to add to the composite filter</param>
    /// <param name="filter7">A filter to add to the composite filter</param>
    /// <param name="filter8">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4,
        Filter filter5,
        Filter filter6,
        Filter filter7,
        Filter filter8
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4,
        filter5,
        filter6,
        filter7,
        filter8
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    /// <param name="filter5">A filter to add to the composite filter</param>
    /// <param name="filter6">A filter to add to the composite filter</param>
    /// <param name="filter7">A filter to add to the composite filter</param>
    /// <param name="filter8">A filter to add to the composite filter</param>
    /// <param name="filter9">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4,
        Filter filter5,
        Filter filter6,
        Filter filter7,
        Filter filter8,
        Filter filter9
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4,
        filter5,
        filter6,
        filter7,
        filter8,
        filter9
    }) {}
	
	/// <summary>
	/// A convenience constructor to allow single-statement
	/// population without the need to pass an array
	/// </summary>
	/// <param name="filter1">A filter to add to the composite filter</param>
	/// <param name="filter2">A filter to add to the composite filter</param>
	/// <param name="filter3">A filter to add to the composite filter</param>
	/// <param name="filter4">A filter to add to the composite filter</param>
    /// <param name="filter5">A filter to add to the composite filter</param>
    /// <param name="filter6">A filter to add to the composite filter</param>
    /// <param name="filter7">A filter to add to the composite filter</param>
    /// <param name="filter8">A filter to add to the composite filter</param>
    /// <param name="filter9">A filter to add to the composite filter</param>
    /// <param name="filter10">A filter to add to the composite filter</param>
    public CompositeFilter(
        Filter filter1,
        Filter filter2,
        Filter filter3,
        Filter filter4,
        Filter filter5,
        Filter filter6,
        Filter filter7,
        Filter filter8,
        Filter filter9,
        Filter filter10
    ) : this(new Filter[] {
        filter1,
        filter2,
        filter3,
        filter4,
        filter5,
        filter6,
        filter7,
        filter8,
        filter9,
        filter10
    }) {}
		
	/// <summary>
	/// Constructs a new instance, adding the specified filters as children
	/// </summary>
	/// <param name="filters">The children to add to this filter</param>
	public CompositeFilter(Filter[] filters) {
	    if (filters == null) {
	        filters = new Filter[] {};
	    }
	    filterList = new ArrayList(filters.Length + 5);
	    Filter filter;
	    for(int x = 0; x < filters.Length; x++) {
	        filter = filters[x];
	        if (filter != null) {
                if (!filterList.Contains(filter)) {
                    filterList.Add(filter);
                }
            }
        }
        filterArray = new Filter[filterList.Count];
        filterList.CopyTo(filterArray);
        isInclusive = true;
    }
    
    /// <summary>
    /// Adds a child filter to this instance
    /// </summary>
    /// <param name="filter">The child to add</param>
    public void AddChild(Filter filter) {
        if (filter == null) {
            throw new ArgumentNullException();
        }
	    lock (monitor) {
	        if (!filterList.Contains(filter)) {
	            filterList.Add(filter);
                Filter[] newChildArray = new Filter[filterList.Count];
                filterList.CopyTo(newChildArray);
                filterArray = newChildArray;
            }
	    }
    }
    
    /// <summary>
    /// Removes the specified child filter from this instance
    /// </summary>
    /// <param name="filter">The child to remove</param>
    public void RemoveChild(Filter filter) {
        if (filter == null) {
            throw new ArgumentNullException();
        }
        lock (monitor) {
            if (filterList.Contains(filter)) {
                filterList.Remove(filter);
                Filter[] newChildArray = new Filter[filterList.Count];
                filterList.CopyTo(newChildArray);
                filterArray = newChildArray;
            }
        }
    }
    
    /// <summary>
    /// Removes all children from this instance
    /// </summary>
	public void ClearChildren() {
        lock (monitor) {
	        filterList.Clear();
	        filterArray = new Filter[0];
        }
    }
    
    /// <summary>
    /// Gets an array containing all child filters used by this instance
    /// </summary>
	public Filter[] Children {
	    get {
	        Filter[] efa = filterArray;
	        Filter[] returnValue = new Filter[efa.Length];
	        Array.Copy(efa, 0, returnValue, 0, efa.Length);
	        return returnValue;
	    }
	}
    
    /// <summary>
    /// Indicates whether the given Event instance is accepted by this filter.  If
    /// this method returns false, the event will not be logged by any loggers using
    /// this filter.
    /// </summary>
    /// <param name="_event">The event to check for acceptability</param>
    /// <returns>true if the event is acceptable, false otherwise</returns>
    public override bool Accepts(Event _event) {
        Filter[] efa = filterArray;
        if (isInclusive) {
            for(int x = 0; x < efa.Length; x++) {
                if (!efa[x].Accepts(_event)) {
                    return false;
                }
            }
            return true;
        }
	    else {
            for(int x = 0; x < efa.Length; x++) {
                if (efa[x].Accepts(_event)) {
                    return true;
                }
            }
            return false;
        }
	}
    
}

}
